package cn.zjitc.controller;


import cn.zjitc.constant.MessageConstant;
import cn.zjitc.entity.PageResult;
import cn.zjitc.entity.QueryPageBean;
import cn.zjitc.entity.Result;
import cn.zjitc.pojo.Book;
import cn.zjitc.service.AdminBookService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/adminbook")
public class AdminBookController {

    @Reference
    private AdminBookService adminBookService;

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody Book book){
        try{
            adminBookService.add(book);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增书籍失败");
        }
        return new Result(true,"新增书籍成功");
    }

    //删除书籍
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            adminBookService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除书籍失败");
        }
        return new Result(true,"删除书籍成功");
    }

    //编辑书籍
    @RequestMapping("/edit")
    public Result edit(@RequestBody Book book){
        try{
            adminBookService.edit(book);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑书籍失败");
        }
        return new Result(true,"编辑书籍成功");
    }

    //查询所有书籍情况
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Book> list= adminBookService.findAll();
            return new Result(true, MessageConstant.ADMIN_SEARCH_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADMIN_SEARCH_FAIL);
        }
    }

    //根据书籍id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Book book = adminBookService.findById(id);
            return new Result(true,MessageConstant.ADMIN_SEARCH_SUCCESS, book);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADMIN_SEARCH_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult= adminBookService.pageQuery(queryPageBean);
        return pageResult;
    }
}
